require(['./config'],()=>{
    require(['template','header','footer','jquery','elevateZoom','jqfly'],(template,header)=>{
        class details{
            constructor(){
                this.render().then(()=>{
                    this.getshoppingcar()
                    this.intocar()
                    this.SubandPlus()
                })
            }
            getshoppingcar(){
                 // elevateZoom这个方法是扩展再$的原型上的
                $('.zoom-image').elevateZoom({
                    gallery: 'gal1' // 放所有小图容器盒子ul的id，不加#
            })
            }
            render(){
                const id = location.search.slice(4)
                return new Promise(resolve => {
                    $.get('http://www.xiongmaoyouxuan.com/api/detail',{ id },resp=>{
                    if(resp.code === 200){
                        const {
                            id,
                            photo,
                            title,
                            price,
                            shop,
                        } = resp.data.detail
                        this.detail = {
                            id,
                            photo,
                            title,
                            price,
                            shop,
                        }
                        $('.left2').html(
                            template('leftTemplate',{
                                ...this.detail
                                
                            })
                        )
                        $('.right2-details').html(
                            template('right2-details-Template',{
                                ...this.detail

                            })
                        )
                        resolve()
                    }
                })
                })
            }
            intocar(){
                //加入购物车使用localStorage来存
                $('#intocar').on('click',(e)=>{
                    this.fly(e)
                    let cart = localStorage.getItem('cart')
                    if(cart){
                        cart = JSON.parse(cart)
                        const isExist = cart.some(shop=> shop.id === this.detail.id)
                        if(isExist){
                            cart = cart.map(shop =>{
                                if(shop.id === this.detail.id){
                                    shop.count=shop.count+($('#sum').val()-0)
                                }
                                return shop
                            })
                        }else{
                           cart.push({
                               ...this.detail,
                               count:1,
                               check:true
                           }) 
                        }
                        localStorage.setItem('cart',JSON.stringify(cart))
                    }else{
                        localStorage.setItem('cart',JSON.stringify([{
                            ...this.detail,
                            count:1,
                            check:true
                        }]))
                    }
                })
            }
            SubandPlus(){
                $('#sub').on('click',()=>{
                    let ss = $('#sum').val()-0
                    if(ss>1){
                        ss--
                        $('#sum').val(ss)
                        let cart = localStorage.getItem('cart')
                        cart = JSON.parse(cart)
                        if(cart){
                            
                            const isEXist = cart.some((item)=>{
                                return item.id === location.search.slice(4)
                            })
                            if(isEXist){
                                cart.map((item)=>{
                                    if(item.id===location.search.slice(4)-0){
                                        console.log(item.count)
                                        item.count = $('#sum').val()-0+item.count
                                    }
                                })
                                localStorage.setItem('cart',JSON.stringify(cart))
                            }
                        }
                    }else{
                        alert('减不动了')
                    }
                })
                $('#plus').on('click',()=>{
                    let ss = $('#sum').val()-0
                    if(ss>1){
                        ss++
                        $('#sum').val(ss)
                        let cart = localStorage.getItem('cart')
                        cart = JSON.parse(cart)
                        if(cart){
                            console.log(cart)
                            const isEXist = cart.some((item)=>{
                                return item.id === location.search.slice(4)
                            })
                            if(isEXist){
                                cart.map((item)=>{
                                    if(item.id===location.search.slice(4)){
                                        item.count = $('#sum').val()-0+item.count
                                    }
                                })
                                localStorage.setItem('cart',JSON.stringify(cart))
                            }
                        }
                    }else{
                        alert('减不动了')
                    }
                })
            }
            fly(e){
                $('<img class="fly" src="/img/money.gif">').fly({
                    start:{
                      left: e.clientX,  //开始位置（必填）#fly元素会被设置成position: fixed
                      top: e.clientY,  //开始位置（必填）
                    },
                    end:{
                      left: $('.details-car').offset().left, //结束位置（必填）
                      top: $('.details-car').offset().top,  //结束位置（必填）
                    //   width: 100, //结束时高度
                    //   height: 100, //结束时高度
                    },
                    // autoPlay: false, //是否直接运动,默认true
                    speed: 1.1, //越大越快，默认1.2
                    vertex_Rtop: 50, //运动轨迹最高点top值，默认20
                    onEnd: function(){
                         this.destroy()
                         header.calcCartCount()
                    } //结束回调
                  });
            }
        }
        new details()
    })
})